import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.odps.udf.UDF;
import com.aliyun.odps.utils.StringUtils;

public class SpliderLancomeGoods extends UDF {

	
	/*
	 * public static void main(String[] args) { String html = TestHtml.html();
	 * evaluate(html); }
	 */
	 
	public String evaluate(String html) {
		JSONObject obj = new JSONObject();
		if (StringUtils.isNotBlank(html)) {
			Document document = Jsoup.parse(html);
			// 获取商品名称 有中文title和英文title
			Elements titClass = document.getElementsByClass("product-tit");
			if (null != titClass && titClass.size() == 1) {
				Elements cnEles = titClass.get(0).getElementsByTag("h1");
				if (cnEles != null && cnEles.size() == 1) {
					String cnTitle = cnEles.get(0).text();
					System.out.println(cnTitle);
					obj.put("cnTitle", cnTitle);
				}
				Elements enEles = titClass.get(0).getElementsByTag("h2");
				if (enEles != null && enEles.size() == 1) {
					String enTitle = enEles.get(0).text();
					System.out.println(enTitle);
					obj.put("enTitle", enTitle);
				}
			}
			//获取产品简分类
			Elements categoryClass = document.getElementsByClass("article article-breadcrumb-wrapper");
			if(null != categoryClass && categoryClass.size() != 0 ) {
				Elements elementsByTag = categoryClass.get(0).getElementsByTag("li");
				if(null != elementsByTag && elementsByTag.size() != 0) {
					String category = elementsByTag.get(elementsByTag.size()-2).text();
					System.out.println("fenlei :"+category);
					obj.put("category", category);
				}
			}

			// 获取产品价格 product-price
			Elements priceClass = document.getElementsByClass("product-price");
			if (null != priceClass && priceClass.size() != 0) {
				String price = priceClass.get(0).text();
				System.out.println(price);
				obj.put("price", price);
			}

			// 获取规格 容量  有的有多个容量规格
			Elements volumeClass = document.getElementsByClass("capacity-num");
			
			if (null != volumeClass && volumeClass.size() >= 2) {
				StringBuffer volumeSb = new StringBuffer();
				for (int i = 1; i < volumeClass.size(); i++) {
					String text = volumeClass.get(i).text();
					volumeSb.append(text).append("@@");
				}
				String volume = volumeSb.substring(0, volumeSb.lastIndexOf("@@"));
				System.out.println(volume);
				obj.put("volume", volume);
			}
			//获取颜色
			Elements colorClass = document.getElementsByClass("get-propertyValue");
			if(null != colorClass && colorClass.size() == 1) {
				//StringBuffer colorSb = new StringBuffer();
				//Elements elementsByTag = colorClass.get(0).getElementsByTag("li");
				//for(Element e :elementsByTag) {
					//colorSb.append(e.text()).append("@@");
				//}
				//String color = colorSb.substring(0, colorSb.lastIndexOf("@@"));
				String color = colorClass.text();
				obj.put("color", color);
			}

			// 获取图片
			Element imgId = document.getElementById("image-detail");
			Elements imgByClass = imgId.getElementsByClass("swiper-wrapper");
			if (null != imgByClass && imgByClass.size() == 1) {
				Elements liByTag = imgByClass.get(0).getElementsByTag("li");
				if (null != liByTag && liByTag.size() != 0) {
					StringBuffer imgSb = new StringBuffer();
					for(Element liEle : liByTag) {
						Elements elementsByTag = liEle.getElementsByTag("img");
						for (Element img : elementsByTag) {
							imgSb = imgSb.append(img.attr("data-src")).append("@@");
						}
					}
					String imgUrl = imgSb.substring(0, imgSb.lastIndexOf("@@"));
					System.out.println(imgUrl);
					obj.put("imgUrl", imgUrl);
				}
			}
			
			//获取产品简介 描述
			StringBuffer sb = new StringBuffer();
			Elements descriptClass = document.getElementsByClass("product-introduction-box none-sm");
			if(null != descriptClass && descriptClass.size() ==1 ) {
				Elements divs = descriptClass.get(0).getElementsByTag("div");
				if(null != divs && divs.size() == 2) {
					String text1 = divs.get(0).text();
					String text2 = divs.get(1).text();
					String replace = text1.replace(text2, "");
					String replace2 = replace.replace("查看更多", "");
					//简介
					sb = sb.append(replace2) ;
				}
			}
			Elements descClass = document.getElementsByClass("tabs-container");
			if(null != descClass && descClass.size() ==1 ) {
				Elements divs = descClass.get(0).getElementsByTag("div");
				if(null != divs && divs.size() == 2) {
					String text1 = divs.get(0).text();
					String text2 = divs.get(1).text();
					String replace = text1.replace(text2, "");
					//简介
					sb = sb.append(replace) ;
				}
			}
			System.out.println(sb);
			obj.put("descript", sb);
			
		}

		System.out.println(obj.toJSONString());
		return obj.toJSONString();
	}
}
